package edu.hebeu.evil.freshmenguide.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import edu.hebeu.evil.freshmenguide.Activity.AboutActivity;
import edu.hebeu.evil.freshmenguide.Activity.AddressListActivity;
import edu.hebeu.evil.freshmenguide.Activity.CarActivity;
import edu.hebeu.evil.freshmenguide.Activity.HanDanView;
import edu.hebeu.evil.freshmenguide.Activity.SchoolMapActivity;
import edu.hebeu.evil.freshmenguide.Activity.WebViewActivity;
import edu.hebeu.evil.freshmenguide.R;
import edu.hebeu.evil.freshmenguide.Utils.Adapter.CommonAdapter;
import edu.hebeu.evil.freshmenguide.Utils.Adapter.ViewHolder;
import edu.hebeu.evil.freshmenguide.Utils.SuperToast.SuperToast;
import edu.hebeu.evil.freshmenguide.Utils.Utils;

/********************************************
 * 工程名：FreshmenGuide
 * 包名：edu.hebeu.evil.freshmenguide.Fragment
 * 时间：16-7-6 下午7:27
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：左侧菜单
 ********************************************/
public class LeftMenuFragment extends Fragment
{
    private static final String[] title = {"乘车指引", "校园地图"
            , "信电学院", "学习生活指南", "邯郸美景", "办公室联系电话", "关于软件"};
    private static final int[] img = {R.drawable.leftmenu_car
            , R.drawable.leftmenu_map, R.drawable.leftmenu_school
            , R.drawable.leftmenu_life, R.drawable.leftmenu_scenery
            , R.drawable.leftmenu_telephone, R.drawable.leftmenu_about};

    private View view;
    private static LeftMenuFragment leftMenuFragment;

    private ImageView leftmenu_background;
    private ListView listView;

    private ArrayList<ItemBean> list;
    private Adapter adapter;


    public static LeftMenuFragment newInstance()
    {
        if (leftMenuFragment == null)
            leftMenuFragment = new LeftMenuFragment();
        return leftMenuFragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.leftmenu_fragment, container, false);
        initView(view);
        initData();
        setListView();
        onClick();
        return view;
    }

    /**
     * 初始化控件
     */
    private void initView(View view)
    {
        leftmenu_background = (ImageView) view.findViewById(R.id.leftmenu_background);
        listView = (ListView) view.findViewById(R.id.listView);
    }

    /**
     * 初始化数据
     */
    private void initData()
    {
        Utils.Glide(R.drawable.leftmenu_background, leftmenu_background);
        list = new ArrayList<>();
        for (int i = 0; i < title.length; i++)
        {
            list.add(new ItemBean(img[i], title[i]));
        }

    }

    /**
     * 设置ListView
     */
    private void setListView()
    {
        adapter = new Adapter(getContext(), list, R.layout.leftmenu_item);
        listView.setAdapter(adapter);
    }

    /**
     * 点击事件
     */
    private void onClick()
    {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                switch (i)
                {
                    case 0:
                        startActivity(new Intent(getContext(), CarActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(getContext(), SchoolMapActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(getContext(), WebViewActivity.class)
                                .putExtra("title", "信电学院").putExtra("url", "file:///android_asset/xindian.html"));
                        break;
                    case 3:
                       /* startActivity(new Intent(getContext(), WebViewActivity.class)
                                .putExtra("title", "学习生活指南").putExtra("url", "file:///android_asset/handanview/index.html"));*/
                        SuperToast.buttonToast(getActivity())
                                .setText("尚未开启，敬请期待")
                                .show();
                        break;
                    case 4:
                        startActivity(new Intent(getContext(), HanDanView.class));
                        break;
                    case 5:
                        startActivity(new Intent(getContext(), AddressListActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(getContext(), AboutActivity.class));
                        break;


                }
            }
        });
    }


    /**
     * ListView适配器
     */
    private class Adapter extends CommonAdapter<ItemBean>
    {
        public Adapter(Context context, List<ItemBean> itemBean_list, int itemLayoutResId)
        {
            super(context, itemBean_list, itemLayoutResId);
        }

        @Override
        public void convert(ViewHolder viewHolder, ItemBean item)
        {
            TextView title = viewHolder.getView(R.id.menuTitle);
            title.setText(item.title);

            ImageView imageView = viewHolder.getView(R.id.menuImg);
            Glide.with(getContext()).load(item.imgPath).into(imageView);
        }
    }


    private class ItemBean
    {
        private int imgPath;
        private String title;

        public ItemBean(int imgPath, String title)
        {
            this.imgPath = imgPath;
            this.title = title;
        }
    }

}





















